﻿using MiniFox.Infrastructure;
using MiniFox.Resources;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Sys.Core.Models.VO
{
    [Table("application")]
    public class ApplicationVo
    {
        [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")]
        [Display(Name = "应用程序代码", ShortName = "应用程序代码")]
        [Column("appId")]
        [JsonProperty("appId")]
        public string ApplicationID { get; set; }

        [Display(Name = "应用程序名", ShortName = "应用程序名")]
        [Column("appName")]
        [JsonProperty("appName")]
        public string ApplicationName { get; set; }

        [Display(Name = "应用程序主机名", ShortName = "主机名")]
        [Column("domain")]
        [JsonProperty("domain")]
        public string ApplicationDomain { get; set; }

        [Display(Name = "应用程序主路径", ShortName = "应用程序主路径")]
        [Column("path")]
        [JsonProperty("path")]
        public string ApplicationPath { get; set; }

        [Display(Name = "端口号", ShortName = "端口号")]
        [Column("port")]
        [JsonProperty("port")]
        public string PortNumber { get; set; }
        [Display(Name = "使用SSL(https)", ShortName = "SSL(https)")]
        [Column("ssl")]
        [JsonProperty("ssl")]
        public bool RequireSSL { get; set; }

        [Display(Name = "系统图标", ShortName = "系统图标")]
        [Column("icon")]
        [JsonProperty("icon")]
        public string Icon { get; set; }

        [Display(Name = "启用", ShortName = "启用")]
        [Column("enabled")]
        [JsonProperty("enabled")]
        public bool Enabled { get; set; }

        [Display(Name = "完整主机名", ShortName = "完整主机名")]
        [Column("fullHost")]
        [JsonProperty("fullHost")]
        public string FullHost
        {
            get
            {
                if (!string.IsNullOrEmpty(this.ApplicationDomain))
                {
                    string host = this.ApplicationDomain.Replace("/", "");
                    string protocal = this.RequireSSL ? "https:" : "http:";
                    string port = (string.IsNullOrEmpty(this.PortNumber) || this.PortNumber.Trim() == "80") ? string.Empty : (":" + PortNumber);
                    return (protocal + "//" + host + port).ToLower();

                }
                else
                {
                    return string.Empty;
                }
            }
        }

        [Display(Name = "完整路径", ShortName = "完整路径")]
        [Column("fullPath")]
        [JsonProperty("fullPath")]
        public string FullPath
        {
            get
            {
                if (string.IsNullOrEmpty(this.ApplicationPath) || this.ApplicationPath.Trim() == "/" || this.ApplicationPath.Trim() == "~/")
                    return FullHost;
                string appPath = this.ApplicationPath;
                if (!appPath.StartsWith("/"))
                    appPath = "/" + appPath;
                while (appPath.EndsWith("/"))
                    appPath = appPath.TrimEnd('/');
                return FullHost + appPath.ToLower();
            }
        }

        [Display(Name = "创建时间", ShortName = "创建时间")]
        [Column("createTime")]
        [JsonProperty("createTime")]
        public DateTime CreateTime { get; set; }

        [Display(Name = "上次修改时间", ShortName = "上次修改时间")]
        [Column("updateTime")]
        [JsonProperty("updateTime")]
        public DateTime UpdateTime { get; set; }
    }
}
